package com.jz.netthings.analysis.task;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jz.netthings.analysis.mapper.ProviderInfoDzjwdMapper;
import com.jz.netthings.analysis.service.NewProviderDisplayService;
import com.jz.netthings.analysis.service.ProviderInfoDzjwdService;
import com.jz.netthings.analysis.service.ProviderProcessService;
import com.jz.netthings.analysis.service.XyDevelopAnalysisService;
import com.jz.netthings.appengine.baseData.ProviderInfo;
import com.jz.netthings.appengine.mapper.ProviderInfoMapper;
import com.jz.netthings.exception.AdscmSystemException;
import com.jz.netthings.utils.AdscmBaseUtils;
import com.jz.netthings.utils.AdscmDateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 定时程序
 */
@Component
public class TimerTaskJob {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    ProviderInfoDzjwdService providerInfoDzjwdService;

    @Autowired
    NewProviderDisplayService newProviderDisplayService;

    @Autowired
    ProviderProcessService providerProcessService;
    @Autowired
    XyDevelopAnalysisService xyDevelopAnalysisService;
    @Autowired
    ProviderInfoDzjwdMapper providerInfoDzjwdMapper;
    @Autowired
    ProviderInfoMapper providerInfoMapper;

    @Autowired
    RedisTemplate<Object , Object> objectRedisTemplate;

    String lastProviderCreateTime;
    /**
     * 10 分钟一次，查找新增浆员，构造浆员对应的地址经纬度信息
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void loadNewProviderDetails() throws AdscmSystemException {
        logger.info("10 分钟一次，查找新增浆员，构造浆员对应的地址经纬度信息任务 。。。。。。");

        if(!AdscmBaseUtils.getInstance().isNotEmpty(lastProviderCreateTime,null)){
            lastProviderCreateTime = AdscmDateUtils.getNowOnlyDateFomat1(new Date());
        }
        //执行操作
        providerInfoDzjwdService.formatDz(lastProviderCreateTime);
        //把时间更新到当前
        lastProviderCreateTime = AdscmDateUtils.getDateFomat(new Date());
    }
    /**
     * 10 分钟一次，查找还没有距离的标准地址，对其进行到浆站距离及预计花费时间的计算
     */
    @Scheduled(cron = "0 0/10 * * * ?")
    public void loadBaseAddressDistance() throws AdscmSystemException {
        logger.info("10 分钟一次，查找还没有距离的标准地址，对其进行到浆站距离及预计花费时间的计算任务 。。。。。。");
        //执行操作
        providerInfoDzjwdService.loadBaseAddressDistance();
    }

    /**
     * 5 分钟一次，构造当天营运统计综合仪表盘
     */
    @Scheduled(cron = "0 0/5 * * * ?")
    public void loadOperationalManage() throws AdscmSystemException {
        logger.info("5 分钟一次，构造当天营运统计综合仪表盘 。。。。。。");
        String nowDate = AdscmDateUtils.getNowOnlyDateFomat1(new Date());
        String dateTime = AdscmDateUtils.getDateFomat(new Date());

        logger.info("构造当天建档浆员的献浆过程时序图，开始...");
        long beginTime = System.currentTimeMillis();
        Wrapper<ProviderInfo> infoWrapper = new EntityWrapper<>();
        infoWrapper.between("createDate",nowDate + " 00:00:00",nowDate + " 23:59:59");

        List<ProviderInfo> infos = providerInfoMapper.selectList(infoWrapper);
        if (infos != null && infos.size()>0){
            for (ProviderInfo info : infos) {
                newProviderDisplayService.loadNewProviderDisplay(info);
            }
        }
        logger.info("构造当天建档浆员的献浆过程时序图，结束，耗时【"+((System.currentTimeMillis() - beginTime)/1000)+"】秒");

        beginTime = System.currentTimeMillis();
        logger.info("构造当前时间的各个环节献浆过程数量统计...");
        providerProcessService.loadProviderProcessByTime(dateTime);


    }

    /**
     * 把当天登记的所有浆员对应的村落，进行详细统计信息的重新计算
     * 每天晚上 23:00 执行
     */
    @Scheduled(cron = "0 0 23 * * ?")
    public void reLoadRegProviderDetails() throws AdscmSystemException{
        logger.info("把当天登记的所有浆员对应的村落，进行详细统计信息的重新计算 任务。。。。。。");
        String beginTime = AdscmDateUtils.getNowOnlyDateFomat1(new Date()) + " 00:00:00";
        String endTime = AdscmDateUtils.getNowOnlyDateFomat1(new Date()) + " 23:59:59";
        List<String> villList = providerInfoDzjwdMapper.selectRegVillList(beginTime, endTime);
        if (villList != null && villList.size()>0){
            for (String vill : villList) {
                providerInfoDzjwdService.loadAllVillageDetailMsg(vill);
            }
        }
    }
    /**
     * 当期与同期浆量、新浆员、返站浆员的综合统计信息，重新统计，更新到Redis中
     * 每天凌晨 04:00 执行
     */
    @Scheduled(cron = "0 0 4 * * ?")
    public void reLoadXyDevelop1() throws AdscmSystemException{
        logger.info("当期与同期浆量、新浆员、返站浆员的综合统计信息，重新统计，更新到Redis中 的 任务。。。。。。");

        AdscmBaseUtils.getInstance().cleanObjFromRedis("xyDevelopAnalysisService.getPulpWeight",objectRedisTemplate);
        AdscmBaseUtils.getInstance().cleanObjFromRedis("xyDevelopAnalysisService.getFrequencyCount",objectRedisTemplate);
        AdscmBaseUtils.getInstance().cleanObjFromRedis("xyDevelopAnalysisService.getNewAndOutProviderCount",objectRedisTemplate);

        xyDevelopAnalysisService.getPulpWeight(null);
        xyDevelopAnalysisService.getFrequencyCount(null);
        xyDevelopAnalysisService.getNewAndOutProviderCount(null);

    }

}
